package Leetcode.lc002;

import Leetcode.tools.ListNode;

/***
 * 链表两数相加
 */
public class Sloution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        //设置哨兵节点
        ListNode dummy = new ListNode();
        ListNode cur=dummy;
        int carry=0;
        while (l1!=null || l2!=null || carry!=0){
            int temp=carry;
            if(l1!=null){
                temp+=l1.val;
                l1=l1.next;
            }
            if(l2!=null){
                temp+=l2.val;
                l2=l2.next;
            }
            carry=temp/10;
            cur.next=new ListNode(temp%10);
            cur=cur.next;
        }
        return dummy.next;
    }

    public static void main(String[] args) {

    }
}
